/*
 * Copyright (C) 2010 Fred Barrie
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.aergia.vinny.gui.view;

import java.awt.Color;

import javax.swing.JCheckBox;
import javax.swing.JTextField;

import org.aergia.vinny.core.ApiResult;
import org.aergia.vinny.database.pojo.VinnyUser;
import org.aergia.vinny.database.pojo.VolunteerGroup;
import org.aergia.vinny.gui.controller.VinnyController;
import org.aergia.vinny.gui.util.FontUtil;
import org.aergia.vinny.gui.util.LabelPanel;
import org.aergia.vinny.gui.view.EditorDialog.EditorPanel;

public class GroupEditor extends EditorPanel {

	private static final long serialVersionUID = 1L;

	private JCheckBox cardActive;
	private final VinnyController controller;
	private VolunteerGroup group;
	private JTextField groupNameField;
	private final VinnyUser user;

	public GroupEditor(final VinnyController controller, final VinnyUser user) {
		this(controller, user, null);
	}

	/**
	 * @param controller
	 * @param volunteer
	 * @param group
	 */
	public GroupEditor(final VinnyController controller, final VinnyUser user,
			final VolunteerGroup group) {
		this.controller = controller;
		this.user = user;
		this.group = group;
		this.initComponents();
	}

	public VolunteerGroup getGroup() {
		return this.group;
	}

	@Override
	public String getTitle() {
		return "Group Editor";
	}

	@Override
	public boolean saveAction() {
		if (this.group == null) {
			this.group = new VolunteerGroup();
		}

		if (!this.verify()) {
			return false;
		}

		this.group.setActive(this.cardActive.isSelected());
		this.group.setName(this.groupNameField.getText());
		this.saveGroup();
		return true;
	}

	private void initComponents() {

		this.cardActive = new JCheckBox();
		this.cardActive.setName("Active?");
		this.cardActive.setSelected(this.group != null ? this.group.isActive()
				: true);

		this.groupNameField = new JTextField(
				this.group != null ? this.group.getName() : "");
		this.groupNameField.setName("Group Name");

		this.add(new LabelPanel(FontUtil.getLabelFont(), Color.black,
				this.cardActive, this.groupNameField));
		this.setVisible(true);
	}

	private void saveGroup() {
		final ApiResult<VolunteerGroup> result = this.controller.saveGroup(
				this.group, this.user);
		if (!result.isError()) {
			this.group = result.getResult();
		}
	}

	private boolean verify() {
		return true;

	}
}
